package com.goldsprite.infinityworld.ui;

import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.*;
import java.util.*;

import com.badlogic.gdx.utils.Align;
import com.goldsprite.gdxcore.logs.LogViewerService;
import com.goldsprite.gdxcore.screens.GScreen;
import com.goldsprite.gdxcore.ui.GSplitPane;
import java.util.List;
import com.goldsprite.gdxcore.logs.RealtimeInfoViewer;

public class EditorLayout extends Table{
	public Skin skin;

	float margin = 8;
	float defaultSplitRatio = 0.15f;

	private Table inspectorPanel, inspectorPanelContent;
	private TextButton inspectorPanelTitle;

	private LogViewerService logViewer;


	public Table init(GScreen screen, Skin skin, WidgetGroup gameTable, Runnable initGameWorld){
		this.skin = skin;
		//logViewer
		{
			logViewer = new LogViewerService(skin);
			LogViewerService.log("logViewer初始化.\n\n\n\n");
			LogViewerService.log("视口: " + screen.getViewSize());
		}

		// 左侧层级视图（Hierarchy）
		Tree<MyNode, Object> hierarchyTree = new Tree<>(skin);
		MyNode rootNode = new MyNode(new Label("Root", skin));
		hierarchyTree.add(rootNode);

		// 添加一些嵌套节点
		for (int i = 1; i <= 5; i++) {
			MyNode childNode = new MyNode(new Label("Child " + i, skin));
			rootNode.add(childNode);
		}
		rootNode.setExpanded(false);  // 默认展开

		// 左侧滚动视图包裹树
		ScrollPane hierarchyScrollPane = new ScrollPane(hierarchyTree, skin);
		hierarchyScrollPane.getStyle().background = skin.getDrawable("list");

		// 创建 SplitPane，包含左侧的层级视图和右侧的分栏
		GSplitPane hierarchySplit = new GSplitPane(hierarchyScrollPane, gameTable, false, skin);
		hierarchySplit.setToggleData(0, defaultSplitRatio);
		hierarchySplit.toggle(false);

		//创建console板
		Table consolePane = new Table(skin);
		consolePane.setFillParent(true);
		consolePane.top().left();
		consolePane.setBackground("list");
		consolePane.add(logViewer.getRootLayout()).expand().fill();

		Container<Table> consoleContainer = new Container<>(consolePane);
		consoleContainer.minSize(0);
		consoleContainer.bottom().left();
		
		//实时信息板
		Table realtimeInfoTable = new Table(skin);
		realtimeInfoTable.setBackground("list");
		realtimeInfoTable.top().left();
		RealtimeInfoViewer infoViewer = new RealtimeInfoViewer(skin);
		realtimeInfoTable.add(infoViewer.getLabel());
		ScrollPane realtimeInfoScroll =new ScrollPane(realtimeInfoTable);
		SplitPane realtimeInfoSplit = new SplitPane(consoleContainer, realtimeInfoScroll, false, skin);
		realtimeInfoSplit.setSplitAmount(0.5f);

		//创建上下分割板
		GSplitPane consoleSplit = new GSplitPane(realtimeInfoSplit, hierarchySplit, true, skin);
		//consoleSplit.setDebug(true, true);
		//consoleSplit.setAbsoluteSplitAmount(0f);
		consoleSplit.setToggleData(0, defaultSplitRatio);
		consoleSplit.toggle(false);

		// 创建右侧的Inspector面板区域
		inspectorPanel = new Table(skin);
		inspectorPanel.setBackground("list");  // 设置淡灰色背景
		inspectorPanel.pad(10);  // 增加内边距
		inspectorPanel.setFillParent(true);
		inspectorPanel.top().left();
		inspectorPanelTitle = new TextButton("Inspector", skin);
		marginLRB(inspectorPanelTitle, margin);
		inspectorPanel.add(inspectorPanelTitle).expandX().fillX().row();
		inspectorPanelContent = new Table(skin);
		inspectorPanel.add(inspectorPanelContent).expand().fill();

		//容器
		Container<Table> inspectorContainer = new Container<>(inspectorPanel);
		inspectorContainer.bottom().left();
		inspectorContainer.minWidth(0);

		// 创建右侧的分栏，包含游戏视图和Inspector面板
		GSplitPane inspectorSplit = new GSplitPane(consoleSplit, inspectorContainer, false, skin);
		inspectorSplit.setToggleData(1, 1-defaultSplitRatio);
		inspectorSplit.toggle(false);

		//总布局
		Table rootTable = this;
		rootTable.top().left();
		rootTable.setFillParent(true);

		//增加一个顶部menu
		Table menuTable = new Table(skin);
		menuTable.setBackground("list");
		marginLRB(menuTable, margin);
		menuTable.left();

		Object[] menuBtnData = {
			"Hierarchy", hierarchySplit, 
			"Console", consoleSplit, 
			"Inspector", inspectorSplit, 
		};
		for(int i=0;i<menuBtnData.length;i+=2){
			String text = (String)menuBtnData[i];
			GSplitPane pane = (GSplitPane)menuBtnData[i+1];
			TextButton cgBtn = new TextButton(text, skin);
			marginLRB(cgBtn, margin);
			cgBtn.addListener(new ClickListener(){
					public void clicked(InputEvent ev, float x, float y){
						pane.toggle();
					}
				});
			
			menuTable.add(cgBtn);
			
		}
		//右上角开始按钮
		{
			menuTable.add().expand();
			TextButton startBtn = new TextButton("I>", skin);
			marginLRB(startBtn, margin);
			startBtn.setWidth(50);
			startBtn.addListener(new ClickListener(){
					public void clicked(InputEvent ev, float x, float y){
						if(initGameWorld != null) initGameWorld.run();
					}
				});
			menuTable.add(startBtn);
		}
		rootTable.add(menuTable).expandX().fillX().row();

		// 将 SplitPane 添加到整个布局
		rootTable.add(inspectorSplit).expand().fill();

		// 监听树的选择变化
		hierarchyTree.addListener(
			new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					// 获取选中的节点
					MyNode selectedNode = (MyNode) hierarchyTree.getSelection().first();
					if (selectedNode != null) {
						updateInspector(selectedNode);  // 更新Inspector面板
					}
				}
			}
		);

		return this;
	}

	private <T extends Table> void marginLRB(T layout, float margin) {
		layout.padLeft(margin);
		layout.padRight(margin);
		layout.padBottom(margin);
	}

	// 更新 Inspector 面板的内容
	private void updateInspector(MyNode selectedNode) {
		// 清空当前的 Inspector 内容
		inspectorPanelContent.clearChildren();
		inspectorPanelContent.defaults().space(margin);
		inspectorPanelContent.top().left();

		// 添加新内容（例如显示节点的属性）
		Label nameLabel = new Label("Name: " + selectedNode.name, skin);
		inspectorPanelContent.add(nameLabel).expandX().fillX().row();

		// 可以为每个属性添加输入框、选择框等控件
		TextField positionField = new TextField("100, 100", skin);  // 示例字段
		positionField.setMessageText("位置属性");
		inspectorPanelContent.add(positionField).expandX().fillX().row();

		TextField positionField2 = new TextField("ndhskwk", skin);  // 示例字段
		positionField2.setMessageText("xjd属性");
		inspectorPanelContent.add(positionField2).expandX().fillX().row();
	}


	public static class MyNode extends Tree.Node<Tree.Node, Object, Actor> {
		public String name = "";

		public MyNode(Label actor) {
			super(actor);
			name = actor.getText().toString();
		}
	}

}
